<?php
namespace app\api\model;

use app\common\helper\Obj2Array;
use app\common\model\project_model\AlbumSkinCategoryModel;
use app\common\model\project_model\AlbumSkinModel as AlbumSkin;

class AlbumSkinModel extends AlbumSkin {

    /**
     * 获取模板类型及所有的模板
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function skinCategory() {
       $cate = AlbumSkinCategoryModel::categoryList();
       $albumSkinM = new self();
       $albumSkin = $albumSkinM->field('id, name, title, is_hot')->where(['status' => self::STATUS_OK])->order(['sort' => 'ASC'])->select();
       if (!empty($albumSkin)) {
           $albumSkin = Obj2Array::resObject2Array($albumSkin);
       }

       $data = [];
       $data['cate'] = $cate;
       $data['album_skin'] = $albumSkin;
       return $data;
    }

    /**
     * 通过cate_id获取分类下的所有模板
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function skinByCateId($id) {
        $skinM = new self();
        $skin = $skinM
            ->field('created_time, updated_time, status', true)
            ->where(['status' => self::STATUS_OK, 'cate_id' => $id])
            ->order(['sort' => 'ASC'])
            ->select();
        if (!empty($skin)) {
            $skin = Obj2Array::resObject2Array($skin);
        }

        return $skin;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function skinById($id) {
        $skinM = new self();
        return $skin = $skinM->field('sort, created_time, updated_time, status', true)->where(['id' => $id])->find();
    }
}